package com.xaicode.auth.domain.params;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.Objects;

/**
 * 时间范围
 * 
 * @date 2024/12/23
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TimeRangeParam {

    /**
     * 查询开始时间
     *
     * @see #startTimeMillis()
     */
    private Date startTime;

    /**
     * 查询结束时间
     *
     * @see #endTimeMillis()
     */
    private Date endTime;

    // ----

    /**
     * 获取开始时间，若未传则默认以 2000 年为时间
     *
     * @return .
     */
    public Date getStartTime() {
        if (Objects.nonNull(this.endTime)) {
            return this.endTime;
        }
        return DateUtil.parseDate("2000-01-01");
    }

    /**
     * 获取结束时间，若未传则默认以当前时间向后推一天作为结束时间
     *
     * @return .
     */
    public Date getEndTime() {
        if (Objects.nonNull(this.endTime)) {
            return this.endTime;
        }
        return DateUtil.offsetDay(new Date(), 1);
    }

    // ----

    /**
     * 查询开始时间的毫秒数
     * <p>
     * 若未设置查询开始时间，则默认返回 0
     *
     * @return .
     */
    public long startTimeMillis() {
        if (Objects.isNull(this.startTime)) {
            return 0;
        }
        return this.startTime.getTime();
    }

    /**
     * 查询结束时间的毫秒数
     * <p>
     * 若未设置查询结束时间，则默认返回 {@link Integer#MAX_VALUE}
     *
     * @return .
     */
    public long endTimeMillis() {
        if (Objects.isNull(this.endTime)) {
            return Long.MAX_VALUE;
        }
        return this.endTime.getTime();
    }

    // ----

    /**
     * 获取格式化的起始时间
     *
     * @return yyyy-MM-dd
     */
    public String startTimeFormatNormalDate() {
        Date s = getStartTime();
        return DateUtil.format(s, DatePattern.NORM_DATE_PATTERN);
    }

    /**
     * 获取格式化的起始时间
     *
     * @return yyyyMMdd
     */
    public String startTimeFormatPureDate() {
        Date s = getStartTime();
        return DateUtil.format(s, DatePattern.PURE_DATE_PATTERN);
    }

    /**
     * 获取格式化的结束时间
     *
     * @return yyyy-MM-dd
     */
    public String endTimeFormatNormalDate() {
        Date s = getEndTime();
        return DateUtil.format(s, DatePattern.NORM_DATE_PATTERN);
    }

    /**
     * 获取格式化的结束时间
     *
     * @return yyyyMMdd
     */
    public String endTimeFormatPureDate() {
        Date s = getEndTime();
        return DateUtil.format(s, DatePattern.PURE_DATE_PATTERN);
    }

    // ----

    /**
     * 设置查询时间为本日
     */
    public static TimeRangeParam buildQueryToday() {
        TimeRangeParam set = new TimeRangeParam();
        Date now = new Date();
        set.setStartTime(DateUtil.beginOfDay(now));
        set.setEndTime(DateUtil.endOfDay(now));
        return set;
    }

    /**
     * 设置查询时间为昨日
     */
    public static TimeRangeParam buildQueryYesterday() {
        TimeRangeParam set = new TimeRangeParam();
        DateTime yesterday = DateUtil.yesterday();
        set.setStartTime(DateUtil.beginOfDay(yesterday));
        set.setEndTime(DateUtil.endOfDay(yesterday));
        return set;
    }

    /**
     * 设置查询时间为当日之前的七天
     */
    public static TimeRangeParam buildQueryLastSevenDay() {
        TimeRangeParam set = new TimeRangeParam();
        DateTime yesterday = DateUtil.yesterday();
        DateTime offset7 = DateUtil.offsetDay(yesterday, -6);
        set.setStartTime(DateUtil.beginOfDay(offset7));
        set.setEndTime(DateUtil.endOfDay(yesterday));
        return set;
    }

    /**
     * 设置查询时间为当日之前的指定天数
     * <p>
     * 如，offsetDay=10，即查询过去的十天
     *
     * @param offsetDay 过去天数，不区分正负，使用数字绝对值进行计算
     */
    public static TimeRangeParam buildQueryLastDay(int offsetDay) {
        if (offsetDay == 0) {
            return buildQueryToday();
        }
        if (offsetDay < 0) {
            offsetDay = -1 * offsetDay;
        }
        TimeRangeParam set = new TimeRangeParam();
        DateTime yesterday = DateUtil.yesterday();
        DateTime offset7 = DateUtil.offsetDay(yesterday, -1 * (offsetDay - 1));
        set.setStartTime(DateUtil.beginOfDay(offset7));
        set.setEndTime(DateUtil.endOfDay(yesterday));
        return set;
    }

    /**
     * 设置查询时间为本周
     */
    public static TimeRangeParam buildQueryCurWeek() {
        TimeRangeParam set = new TimeRangeParam();
        Date now = new Date();
        set.setStartTime(DateUtil.beginOfWeek(now, true));
        set.setEndTime(DateUtil.endOfWeek(now, true));
        return set;
    }

    /**
     * 设置查询时间为本月
     */
    public static TimeRangeParam buildQueryCurMonth() {
        TimeRangeParam set = new TimeRangeParam();
        Date now = new Date();
        set.setStartTime(DateUtil.beginOfMonth(now));
        set.setEndTime(DateUtil.endOfMonth(now));
        return set;
    }

    /**
     * 设置查询时间为上个月
     */
    public static TimeRangeParam buildQueryLastMonth() {
        TimeRangeParam set = new TimeRangeParam();
        Date now = new Date();
        DateTime lastMonth = DateUtil.offsetMonth(now, -1);
        set.setStartTime(DateUtil.beginOfMonth(lastMonth));
        set.setEndTime(DateUtil.endOfMonth(lastMonth));
        return set;
    }

    /**
     * 设置查询时间为本年
     */
    public static TimeRangeParam buildQueryCurYear() {
        TimeRangeParam set = new TimeRangeParam();
        Date now = new Date();
        set.setStartTime(DateUtil.beginOfYear(now));
        set.setEndTime(DateUtil.endOfYear(now));
        return set;
    }

    /**
     * 设置查询时间为上一年
     */
    public static TimeRangeParam buildQueryLastYear() {
        TimeRangeParam set = new TimeRangeParam();
        Date now = new Date();
        DateTime lastYear = DateUtil.offsetDay(now, -1 * DateUtil.dayOfYear(now) - 1);
        set.setStartTime(DateUtil.beginOfYear(lastYear));
        set.setEndTime(DateUtil.endOfYear(lastYear));
        return set;
    }

}
